<?php
/**
 * Splits an array into groups
 * 
 *Given a SQl result array[x][y] where x = row and y's key is = col name it will 
 *split it into a new array. SQL must be ordered by the col that result will be 
 *grouped by.
 *
 *@param array to be split
 *@param string col name to split by
 *@param int how meny sub items a group can have 0 = unlimited 
 *@return array  
 */      
function array_group($array, $group_by, $limit=0){

	$count = 0;
	$limit_count = 0;
	$last = "";
	foreach ($array as $row){
		if ($last != $row[$group_by]){
  		$group_by_item = $row[$group_by];
  		$count = 0;
  		$limit_count = 0;
  			foreach ($row as $key => $item){
				if (($key != $group_by)&&(($limit_count < $limit)||($limit == 0))){
					$return[$group_by_item][$count][$key] = $item;
				}
			}
		}else{
			foreach ($row as $key => $item){
				if (($key != $group_by)&&(($limit_count < $limit)||($limit == 0))){
					$return[$group_by_item][$count][$key] = $item;
				}
			}
		}
		$limit_count++;
		$count++;
		$last = $row[$group_by];
	}
	return $return;
}
?>
